

package frontera;


import Control.*;
import Entidad.Compra;
import Entidad.Sistema;
import Entidad.Tarea;
import Entidad.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

/**
 *
 * @author Stiv
 */
public class PanelTareas extends javax.swing.JPanel {

    public static DefaultTableModel dtmtareas;
    public int idTarea = 0;
    //static InformacionTarea IT = new InformacionTarea();
    
    
    
    
    
    public PanelTareas() {
        initComponents();
        //BaseDatos();
        inicializacion();
        
    }
    
    public boolean isCellEditable (int row, int column)
        {
       // Aquí devolvemos true o false según queramos que una celda
       // identificada por fila,columna (row,column), sea o no editable
       if (column == 4)
          return true;
       return false;
   }
    

    public void inicializacion(){

        //this.setSize(1200, 800);
        NuevaTareaBT.setVisible(true);
        dtmtareas = new DefaultTableModel(new Object[][] {
		}, new String[] { "No.","Nombre", "Estado","Fecha Limite", "Tipo", "Asignada"}){
                };
        TablaTareas.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 

        TableColumnModel tcm = TablaTareas.getColumnModel();

        
        DiseñoColumna DC = new DiseñoColumna(Color.orange);
        
        
        
        
        

        
        
        
        
        
      

       TablaTareas.setModel(dtmtareas);    
       
      
       for(Entidad.Tarea p: CrudTarea.bringAll()){
           
           dtmtareas.addRow(new Object[]{p.getID(),p.getNombre() , p.getEstado(),p.getFecha_fin().getTime(),
           p.getTipo(), p.getEmpleado()});
       }
               
       TablaTareas.setDefaultRenderer(Object.class, new FormatoTabla());
       tcm.getColumn(0).setCellRenderer(DC);
 
       
    }
    
    
    public void diseño(){
        TablaTareas.setDefaultRenderer(Object.class, new FormatoTabla());
    }
    
   
    
    
    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        TablaTareas = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        NuevaTareaBT = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        ListaRol = new javax.swing.JComboBox();

        setPreferredSize(new java.awt.Dimension(1000, 869));

        TablaTareas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        TablaTareas.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                TablaTareasKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(TablaTareas);

        NuevaTareaBT.setText("Nueva Tarea");
        NuevaTareaBT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NuevaTareaBTActionPerformed(evt);
            }
        });

        jLabel1.setText("Buscar Tareas por TIPO:");

        ListaRol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Servicio al Ciente", "Servicio Tecnico", "Despacho", "Call Center", "Vendedor", "Todas" }));
        ListaRol.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ListaRolActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 980, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(ListaRol, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(NuevaTareaBT))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(ListaRol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 642, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addComponent(NuevaTareaBT)
                .addGap(74, 74, 74))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void NuevaTareaBTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NuevaTareaBTActionPerformed
        CrearTarea nueva = new CrearTarea();
       nueva.setVisible(true);
        
    }//GEN-LAST:event_NuevaTareaBTActionPerformed

    private void TablaTareasKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TablaTareasKeyPressed
        if (evt.getKeyChar() == KeyEvent.VK_ENTER){
        try{
            int fila = TablaTareas.getSelectedRow();
            int id = 0;
              try{   
                  id = (int)dtmtareas.getValueAt(fila,0);
              }
              catch(Exception g){
              }
              idTarea = id;
              if(idTarea != 0){
                  VentanaInf ventana = new VentanaInf(idTarea);
                  ventana.setVisible(true);
              }
              else JOptionPane.showMessageDialog(TablaTareas, "Seleccione Tarea");
              evt.consume();
        }
        catch(Exception e){
            System.out.print("exception");
            e.printStackTrace();
            
        }
        
    }//GEN-LAST:event_TablaTareasKeyPressed

    }
    
        private void ListaRolActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ListaRolActionPerformed
        String tipo = (String)ListaRol.getSelectedItem();
        limpiarTabla(TablaTareas);
        if(!"Todas".equals(tipo)){
            for(Tarea p : CrudTarea.bringAll()){
                if(p.getTipo().equals(tipo)){
                    dtmtareas.addRow(new Object[]{p.getID(),p.getNombre() , p.getEstado(),p.getFecha_fin().getTime(),
                    p.getTipo(), p.getEmpleado()});
                }
            }
        }
        else {
            for(Entidad.Tarea p: CrudTarea.bringAll()){
                dtmtareas.addRow(new Object[]{p.getID(),p.getNombre() , p.getEstado(),p.getFecha_fin().getTime(),
                p.getTipo(), p.getEmpleado()});
            }
        }
        
        
        
    }//GEN-LAST:event_ListaRolActionPerformed
    
    

        //Limpiar Filas de Tabla
        public void limpiarTabla(JTable tabla){
	        try {
	            DefaultTableModel modelo=(DefaultTableModel) tabla.getModel();
	            int filas=tabla.getRowCount();
	            for (int i = 0;filas>i; i++) {
	                modelo.removeRow(0);
	            }
	        } catch (Exception e) {
	            JOptionPane.showMessageDialog(null, "Error al limpiar la tabla.");
	        }
	    }

        

    
    

    public JTable getTabla(){
        return TablaTareas;
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox ListaRol;
    private javax.swing.JButton NuevaTareaBT;
    private javax.swing.JTable TablaTareas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables

    private void BaseDatos() {
        
        CrudCliente.crearCliente("Damian Diaz",807655,"Cll 210 n° 45-24",2253775);
        //Sistema.mCliente.add(nuevo);
        
        CrudCliente.crearCliente("Alejandro Fabbri",40456709,"Calle 97, Cra 23-45",4876564);
        
        
        CrudProducto.CrearProducto("Locion Lacoste",120000, "100 dias");
       
        CrudCompra.crearCompra(800000, "26/Oct/2012", CrudProducto.bringAll(),CrudCliente.consultarCliente(807655));
        
        System.out.println(CrudCompra.consultarCompra(1).getCliente().getNombre());
        
        CrudCompra.crearCompra(90400, "24/Oct/2012", CrudProducto.bringAll(), CrudCliente.consultarCliente(807655));

           
        Calendar a = Calendar.getInstance();
        
        a.set(2012, 8 , 26);
        
        CrudEmpleado.crearEmpleado(80547654,"Martin Angelo","Despacho","690");
        
        
        CrudTarea.crearTarea("Enviar Mercancia","Creada", a, a,"Envio","envio de mercancia al cliente en las horas de la mañana",
                CrudCompra.consultarCompra(1),CrudEmpleado.consultarEmpleado(80547654).getNombre());
        CrudTarea.crearTarea("Mantenimiento de Producto","Creada", a, a,"Servicio Tecnico","Mantenimiento de mercancia del cliente, por fallos en el encendido del producto",
               CrudCompra.consultarCompra(2),CrudEmpleado.consultarEmpleado(80547654).getNombre());
    }
}
